<?php
class Agency extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('agency_model','agency');
	}
	
	function index()
	{
		if(!empty($_GET['search']))
		{
			$this->agency->where('agencies.name like \'%'.$_GET['search'].'%\'');
		}
		$data['result'] = $this->agency->get();
		$data['pagination'] = $this->agency->pagination();
		$this->template->build('agency_index',$data);
	}
	
	function form($id = NULL)
	{
		$data['rs'] = $this->agency->get_row($id);
		$this->template->build('agency_form',$data);
	}
	
	function save()
	{
		if($_POST)
		{
			if(empty($_POST['id']))
			{
				add_log('เพิ่มหน่วยงาน "'.$_POST['name'].'"');
			}
			else
			{
				add_log('แก้ไขหน่วยงาน "'.$_POST['name'].'"');
			}
			$this->agency->save($_POST);
			set_notify('success', lang('save_data_complete'));
		}
		redirect('user/agency');
	}
	
	function delete($id)
	{
		$rs = $this->agency->get_row($id);
		add_log('ลบหน่วยงาน "'.$rs['name'].'"');
		$this->agency->delete($id);
		set_notify('success', lang('delete_data_complete'));
		redirect($_SERVER['HTTP_REFERER']);
	}
	
}
?>